package com.xpgk.mvc.ddc.zydj.ywsl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.ddc.ddc_commomUtil.DDC_CommonUtilService;
import com.xpgk.mvc.ddc.ddc_commomUtil.PictureService;
import com.xpgk.mvc.ddc.entity.Business;
import com.xpgk.mvc.ddc.entity.Credentials;
import com.xpgk.mvc.ddc.entity.Owner;
import com.xpgk.mvc.ddc.entity.Plate;
import com.xpgk.mvc.ddc.entity.Vehicle;
import com.xpgk.mvc.ddc.entity.VehicleParemeters;
import com.xpgk.mvc.ddc.statistic.DailyStasticService;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.user.User;
import com.xpgk.util.SerialNumberUtil;

public class ZydjYwslController  extends  BaseController{
	private DailyStasticService dailyStasticService;
	private PictureService pictureService = new PictureService();
	private DDC_CommonUtilService commonUtilService = new DDC_CommonUtilService();
	
	public void index() {
		if(!getCUserOrganIds().equals("f73bfba518ff4048afbdc006e3eda906")){
			setAttr("orgid", getCUserOrganIds());
		}
		render("ywsllist.html");
	}
	
	public  void  getlist(){
		String json = paging(splitPage, Vehicle.sqlId_splitPageSelect,Vehicle.sqlId_splitPageFrom2);
		renderJson(json);
	}
	
	public  void  details(){
		ids=getPara("ids");
		Vehicle vehicle = Vehicle.dao.findById(ids);
		setAttr("owner", Owner.dao.findById(vehicle.get("OWNERIDS")));
		setAttr("vehicleparemeters", VehicleParemeters.dao.findById(vehicle.get("VEHICLEPAREMETERSIDS")));
		setAttr("plate", Plate.dao.findById(vehicle.get("PLATEIDS")));
		render("details.html");
	}
	
	public  void  ywsl(){
		Organization  org =  Organization.dao.findById(getCUserOrganIds());
		setAttr("LSH",SerialNumberUtil.getSerialNumber(org, "02"));
		ids=getPara("ids");
		Vehicle vehicle = Vehicle.dao.findById(ids);
		setAttr("vehicle", vehicle);
		setAttr("owner", Owner.dao.findById(vehicle.get("OWNERIDS")));
		setAttr("vehicleparemeters", VehicleParemeters.dao.findById(vehicle.get("VEHICLEPAREMETERSIDS")));
		setAttr("plate", Plate.dao.findById(vehicle.get("PLATEIDS")));
		render("ywsl.html");
	}

	@Before(Tx.class)
	@SuppressWarnings("static-access")
	public  void  saveInfo(){	
		RenderBean result = new RenderBean();
		
		try {
			User user = getCUser();
			String jbrids = user.getStr(User.column_ids);
			String jbr = user.getStr(User.column_name);
			String organids = user.getStr(User.column_organizationids);
			Organization organization = Organization.dao.findById(organids);
			String organ = organization.getStr(Organization.column_name);
			
			SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			Business  business = getModel(Business.class,"business");
			Vehicle vehicle = Vehicle.dao.findById(business.get("VEHICLEIDS"));
			VehicleParemeters  vehicleparemeters = VehicleParemeters.dao.findById(vehicle.get("VEHICLEPAREMETERSIDS"));
			Owner  owner2 = Owner.dao.findById(vehicle.get("OWNERIDS"));
			Owner  owner = getModel(Owner.class,"owner");
			Credentials credentials2 = Credentials.dao.findById(vehicle.get("CREDENTIALSIDS"));;
			Plate  plate2 = Plate.dao.findById(vehicle.get("PLATEIDS"));
			Plate  plate = getModel(Plate.class,"plate");
			plate.set(Plate.column_HPHM, "晋B"+plate.get("HPHM"));
			List<Plate> lp = commonUtilService.getPlateByHPHM(plate.get("HPHM").toString());
			if(lp.size()>0){
				result.setCode(ConstantRender.render_error_code);
				result.setMsg("号牌号码已存在！");
			}else{
				vehicle.set(vehicle.column_HPHM, plate.get("HPHM"));
				vehicle.set(vehicle.column_OWNERNAME, owner.get("OWNERNAME"));
				vehicle.set(vehicle.column_OWNERTYPE, owner.getStr(Owner.column_OWNERTYPE));
				vehicle.set(vehicle.column_BUSINESSTYPE,"2");
				vehicle.update();
				owner.set(owner.column_VEHICLEIDS, vehicle.get("IDS"));
				owner.set(owner.column_CREATEDATE, sdf.format(new Date()));
				owner.set(Owner.column_JBRIDS, jbrids);
				owner.save();
				
				vehicleparemeters.set(vehicleparemeters.column_HPHM, plate.get("HPHM"));
				vehicleparemeters.update();
				
				plate2.set(Plate.column_STATUS, "3").update();
				plate.set(plate.column_VEHICLEIDS, vehicle.get("IDS"));
				plate.set(plate.column_STATUS, "1");
				plate.set(plate.column_CREATEDATE, sdf.format(new Date()));
				plate.set(plate.column_JBRIDS, jbrids);
				plate.set(plate.column_ORGANIDS, organids);
				plate.save();
				
				credentials2.set(Credentials.column_CANCELDATE, sdf.format(new Date()));
				credentials2.set(Credentials.column_STATUS, "3").update();
				Credentials credentials = new Credentials();
				credentials.set(credentials.column_HPHM, plate.get("HPHM"));
				credentials.set(credentials.column_DABH, business.get("LSH"));
				credentials.set(credentials.column_VEHICLEIDS, vehicle.get("IDS"));
				credentials.set(credentials.column_CLLX, vehicleparemeters.get("CLLX"));
				credentials.set(credentials.column_OWNERNAME, owner.get("OWNERNAME"));
//		credentials.set(credentials.column_COMPANYNAME, owner.get("COMPANYNAME"));
				credentials.set(credentials.column_ADDRESS, owner.get("ADDRESS"));
				credentials.set(credentials.column_DETAILADDRESS, owner.get("DETAILADDRESS"));
				credentials.set(credentials.column_SYXZ, vehicleparemeters.get("SYXZ"));
				credentials.set(credentials.column_CLPP, vehicleparemeters.get("CLPP"));
				credentials.set(credentials.column_CLXH, vehicleparemeters.get("CLXH"));
				credentials.set(credentials.column_ZCZL, vehicleparemeters.get("ZCZL"));
				credentials.set(credentials.column_HDZRS, vehicleparemeters.get("HDZRS"));
				credentials.set(credentials.column_CLXH, vehicleparemeters.get("CLXH"));
				credentials.set(credentials.column_STATUS,"1");
				credentials.set(credentials.column_CREATEDATE, sdf.format(new Date()));
				credentials.set(credentials.column_ORGANIDS,organids);
				credentials.set(credentials.column_ORGAN,organ);
				credentials.set(credentials.column_JBRIDS,jbrids);
				credentials.set(credentials.column_JBR,jbr);
				credentials.save();
				
				business.set(business.column_TYPE, "2");
				business.set(business.column_VEHICLEIDS, vehicle.get("IDS"));
				business.set(business.column_OLDCREDENTIALSIDS, credentials2.get("IDS"));
				business.set(business.column_NEWCREDENTIALSIDS, credentials.get("IDS"));
				business.set(business.column_STATUS, "1");
				business.set(business.column_OLDOWNERIDS, owner2.get("IDS"));
				business.set(business.column_NEWOWNERIDS, owner.get("IDS"));
				business.set(business.column_OLDOWNERNAME, owner2.get("OWNERNAME"));
				business.set(business.column_NEWOWNERNAME, owner.get("OWNERNAME"));
				business.set(business.column_NEWOWNERTYPE, owner.getStr(Owner.column_OWNERTYPE));
				business.set(business.column_OLDOWNERTYPE, owner2.getStr(Owner.column_OWNERTYPE));
				business.set(business.column_NEWCARDNUMBER, owner.getStr(Owner.column_SFZHM));
				business.set(business.column_OLDCARDNUMBER, owner2.getStr(Owner.column_SFZHM));
//		business.set(business.column_OLDCOMPANYNAME, owner2.get("COMPANYNAME"));
//		business.set(business.column_NEWCOMPANYNAME, owner.get("COMPANYNAME"));
				business.set(business.column_NEWPAREMETERSIDS, vehicleparemeters.get("IDS"));
				business.set(business.column_CLLX, vehicleparemeters.get("CLLX"));
				business.set(business.column_CLPP, vehicleparemeters.get("CLPP"));
				business.set(business.column_OLDPLATEIDS, plate2.get("IDS"));
				business.set(business.column_NEWPLATEIDS, plate.get("IDS"));
				business.set(business.column_OLDHPHM, plate2.get("HPHM"));
				business.set(business.column_NEWHPHM, plate.get("HPHM"));
				business.set(business.column_ORGANIDS, organids);
				business.set(business.column_ORGANNAME, organ);
				business.set(business.column_YWSLDATE, sdf.format(new Date()));
				business.set(business.column_YWSLJBRIDS, jbrids);
				business.set(business.column_YWSLJBR, jbr);
				business.set(business.column_CLSBDH, vehicleparemeters.getStr(VehicleParemeters.column_CLSBDH));
				business.save();
				
				Owner.dao.findById(owner.get("IDS")).set(Owner.column_BUSINESSIDS, business.get("IDS")).update();
				Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_PLATEIDS, plate.get("IDS")).update();
				Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_OWNERIDS, owner.get("IDS")).update();
				Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_CREDENTIALSIDS, credentials.get("IDS")).update();
//		flag = Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_VEHICLEVEHICLEPAREMETERSIDS, vehicleparemeters.get("IDS")).update();
				VehicleParemeters.dao.findById(vehicleparemeters.get("IDS")).set(VehicleParemeters.column_PLATEIDS, plate.get("IDS")).update();
				
				dailyStasticService.saveDailyStastic(organids,organ,sdf.format(new Date()),"2","1");
				
				String picStr1 = getPara("picStr1");
				String picStr2 = getPara("picStr2");
				String picStr3 = getPara("picStr3");
				String picStr4 = getPara("picStr4");
				String BUSINESSIDS = business.get("IDS");
				String VEHICLEIDS = vehicle.get("IDS");
				if(picStr1.length()>0){
					pictureService.uploadPhoto(picStr1,BUSINESSIDS,VEHICLEIDS,"PICTURE1","PICTURE1");
				}
				if(picStr2.length()>0){
					pictureService.uploadPhoto(picStr2,BUSINESSIDS,VEHICLEIDS,"PICTURE2","PICTURE2");
				}
				if(picStr3.length()>0){
					pictureService.uploadPhoto(picStr3,BUSINESSIDS,VEHICLEIDS,"PICTURE3","PICTURE3");
				}
				if(picStr4.length()>0){
					pictureService.uploadPhoto(picStr4,BUSINESSIDS,VEHICLEIDS,"PICTURE4","PICTURE4");
				}
				result.setCode(ConstantRender.render_success_code);
				result.setMsg("保存成功!");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setCode(ConstantRender.render_error_code);
			result.setMsg("保存失败");
		}
		renderJson(result);
	}
	
	

}
